#region License
///License Begin
///This program has been developed by: Rodolfo Grave de Peralta, 
///Copyright (C) 2007 Rodolfo Grave de Peralta
///This program is free software; you can redistribute it and/or modify
///it under the terms of the GNU General Public License as published by
///the Free Software Foundation; either version 2 of the License, or
///(at your option) any later version.
///
///This program is distributed in the hope that it will be useful,
///but WITHOUT ANY WARRANTY; without even the implied warranty of
///MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
///GNU General Public License for more details.
///
///You should have received a copy of the GNU General Public License
///along with this program; if not, write to the Free Software
///Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
///License End
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

using TEAM.TimeTracking.Model;

namespace TEAM.TimeTracking.View
{
    /// <summary>
    /// Interaction logic for AdminWindow.xaml
    /// </summary>

    public partial class AdminWindow : System.Windows.Window
    {

        public AdminWindow()
        {
            InitializeComponent();
            ctrlNewTaskParentProject.SelectedValue = null;
        }

        protected void ProjectIsActive_Checked(object sender, RoutedEventArgs e)
        {
            App.CurrentApp.MainWidgetWindow.UpdateCurrentProjectsView();
        }

        protected void ProjectIsActive_Unchecked(object sender, RoutedEventArgs e)
        {
            App.CurrentApp.MainWidgetWindow.UpdateCurrentProjectsView();
        }

        protected void CreateNewProject_Click(object sender, RoutedEventArgs e)
        {
            User currentUser = App.CurrentApp.CurrentModel.CurrentUser;
            Project p = new Project();
            p.Name = ctrlNewProjectName.Text;
            p.IsActive = ctrlNewProjectIsActive.IsChecked == true;
            currentUser.Projects.Add(p);
            App.CurrentApp.Database.Set(currentUser);
            App.CurrentApp.Database.Commit();
        }

        protected void CreateNewTask_Click(object sender, RoutedEventArgs e)
        {
            User currentUser = App.CurrentApp.CurrentModel.CurrentUser;
            Task t = new Task();
            t.Name = ctrlNewTaskName.Text;
            t.IsWorkingTime = ctrlNewTaskIsWorkingTime.IsChecked == true;

            Project parentProject = ctrlNewTaskParentProject.SelectedValue as Project;

            if (parentProject == null)
            {
                currentUser.Tasks.Add(t);
            }
            else
            {
                parentProject.AddTask(t);
            }
            App.CurrentApp.Database.Set(currentUser);
            App.CurrentApp.Database.Commit();
            App.CurrentApp.MainWidgetWindow.UpdateCurrentProjectsView();

            ctrlNewTaskParentProject.SelectedValue = null;

        }

        protected void TaskDelete_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            if (b != null)
            {
                Task t = ((FrameworkElement)b.Parent).DataContext as Task;
                if (t != null)
                {
                    User currentUser = App.CurrentApp.CurrentModel.CurrentUser;
                    Project p = t.ParentProject;
                    if (p == null)
                    {
                        currentUser.Tasks.Remove(t);
                        App.CurrentApp.Database.Set(currentUser.Tasks);
                        App.CurrentApp.Database.Commit();
                    }
                    else
                    {
                        p.RemoveTask(t);
                        App.CurrentApp.Database.Set(p);
                        App.CurrentApp.Database.Commit();
                    }
                    if (t == currentUser.DefaultTaskWhenArriving)
                    {
                        currentUser.DefaultTaskWhenArriving = null;
                        foreach (Task task in p.Tasks)
                        {
                            if (task.IsWorkingTime)
                            {
                                currentUser.DefaultTaskWhenArriving = task;
                            }
                        }
                        App.CurrentApp.Database.Set(currentUser);
                        App.CurrentApp.Database.Commit();
                    }

                    if (t == currentUser.DefaultTaskWhenLeaving)
                    {
                        currentUser.DefaultTaskWhenLeaving = null;
                        foreach (Task task in p.Tasks)
                        {
                            if (!task.IsWorkingTime)
                            {
                                currentUser.DefaultTaskWhenLeaving = task;
                            }
                        }
                        App.CurrentApp.Database.Set(currentUser);
                        App.CurrentApp.Database.Commit();
                    }

                }
            }
        }

        protected void ProjectDelete_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            if (b != null)
            {
                Project p = ((FrameworkElement)b.Parent).DataContext as Project;
                if (p != null)
                {
                    User currentUser = App.CurrentApp.CurrentModel.CurrentUser;
                    currentUser.Projects.Remove(p);
                    App.CurrentApp.Database.Set(currentUser.Projects);
                    App.CurrentApp.Database.Commit();
                }
            }
        }

        protected void SetAsDefaultWhenLeaving_Click(object sender, RoutedEventArgs e)
        {
        }

        protected void SetAsDefaultWhenArriving_Click(object sender, RoutedEventArgs e)
        {
        }

    }
}